package bepviet.android.ui.fragment;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ViewFlipper;
import android.widget.LinearLayout.LayoutParams;
import bepviet.android.engine.DataAdapter;
import bepviet.android.engine.R;
import bepviet.android.ui.PreparedMail;
import bepviet.android.ui.TextContent;

public class Testcontent {
	
	private Activity mcontent;
	private String ten;
	private int ma;
	
	public Testcontent(Activity content,int ma,String ten){
		this.mcontent = content;
		this.ma = ma;
		this.ten = ten;
	}
	
	private TextView text1, text2, text3, txtback, txttitle,clickmonan,clicknguyenlieu,clickthuchien;
	private String s1,s2,s3,s4, contentmail;
	private String ThucHien,GioiThieu,NguyenLieuChuanBi,LoaiMonAn,XuatXu,TenMonAn;
	private DataAdapter dataAdapter;	
	private LinearLayout layoutmain,layoutcontent; 
	private GestureDetector detector;
	private ImageView imageView,imgmail;	
	private byte[] blob;
	
	
	
	public View getView(){
		
		View viewgroup = LayoutInflater.from(mcontent).inflate(R.layout.text_content, null);		
		
		LayoutInflater inflater = (LayoutInflater)mcontent.getSystemService(Context.LAYOUT_INFLATER_SERVICE);		
		final View view = inflater.inflate(R.layout.text_intro,null);
		layoutcontent = (LinearLayout)viewgroup.findViewById(R.id.layoutcontent2);
		layoutmain = (LinearLayout)viewgroup.findViewById(R.id.ln);	
		clickmonan = (TextView)viewgroup.findViewById(R.id.click_monan);
		clicknguyenlieu = (TextView)viewgroup.findViewById(R.id.click_nguyenlieu);
		clickthuchien = (TextView)viewgroup.findViewById(R.id.click_thuchien);
		
		
		
		text1 = (TextView)view.findViewById(R.id.txtIntro);		
		text2 = new TextView(mcontent);
		text3 = new TextView(mcontent);
	
		
		txtback = (TextView)viewgroup.findViewById(R.id.txtback_content);
		txttitle = (TextView)viewgroup.findViewById(R.id.txttitle_content);
	
		imageView = (ImageView)view.findViewById(R.id.imageview);
		imgmail = (ImageView)viewgroup.findViewById(R.id.iconmail);
		
		dataAdapter = new DataAdapter(mcontent);	
		Cursor c ;		
		dataAdapter.OpenDataBase();	
		
		
		
		try{
			c = dataAdapter.QueryData("Select * from MonAn where MaMonAn = '"+ma+"'");
			if(c != null){
				if(c.moveToFirst()){
					do{
						blob = c.getBlob(c.getColumnIndex("HinhAnh"));
						ThucHien = c.getString(c.getColumnIndex("ThucHien"));
						GioiThieu =  c.getString(c.getColumnIndex("GioiThieu"));
						NguyenLieuChuanBi =  c.getString(c.getColumnIndex("NguyenLieuChuanBi"));
					}while(c.moveToNext());
				}
			}
		}catch(Exception ex){}
		
		try{
			c = dataAdapter.QueryData("Select * from LoaiMonAn where MaLoai = " +
							"(Select MaLoai from MonAn where MaMonAn = '"+ma+"')");
			if(c != null){
				if(c.moveToFirst()){
					do{
						LoaiMonAn = c.getString(c.getColumnIndex("TenLoai"));
					}while(c.moveToNext());
				}
			}
		}catch(Exception ex){}
		
		try{
			c = dataAdapter.QueryData("Select * from XuatXu where MaXuatXu = " +
							"(Select MaXuatXu from MonAn where MaMonAn = '"+ma+"')");
			if(c != null){
				if(c.moveToFirst()){
					do{
						XuatXu = c.getString(c.getColumnIndex("VungXuatXu"));
					}while(c.moveToNext());
				}
			}
		}catch(Exception ex){}
		ArrayList<String> NguyenLieu = null;		
		try{
			c = dataAdapter.QueryData(" Select  TenNguyenLieu from NguyenLieu N " +
					"Join MonAn_NguyenLieu MN on MN.MaNguyenLieu = N.MaNguyenLieu " +
					"Join MonAn M on M.MaMonAn = MN.MaMonAn and M.MaMonAn = '"+ma+"' " +
					"Group by TenNguyenLieu Having count(distinct M.MaMonAn)=1");
			if(c != null){
				NguyenLieu = new ArrayList<String>();
				if(c.moveToFirst()){
					do{
						NguyenLieu.add(c.getString(c.getColumnIndex("TenNguyenLieu")));
					}while(c.moveToNext());
				}
			}
		}catch(Exception ex){}
		
		
		
		
		TenMonAn = ten;		
		s1 = " -Tên Món Ăn : "+TenMonAn+"\n -Loại Món Ăn : "+LoaiMonAn+"\n -Xuất Xứ : "+ XuatXu +"\n -Giới Thiệu : \n" + GioiThieu;
		s2 = " NGUYÊN LIỆU CHUẨN BỊ :\n"+NguyenLieuChuanBi;
		s3 = " THỰC HIỆN :\n"+ThucHien;	
		contentmail = s1+ "\n\n" + s2 + "\n\n" + s3 + "\n\n" + s4;
			
		text2.setLayoutParams(new LinearLayout.LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
		text3.setLayoutParams(new LinearLayout.LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
		text1.setPadding(5, 5, 5, 5); text1.setTextSize(17f); text1.setTextColor(0xFF800000);
		text2.setPadding(5, 5, 5, 5); text2.setTextSize(17f); text2.setTextColor(0xFF800000);
		text3.setPadding(5, 5, 5, 5); text3.setTextSize(17f); text3.setTextColor(0xFF800000);
	
		text2.setMovementMethod(new ScrollingMovementMethod().getInstance());
		text3.setMovementMethod(new ScrollingMovementMethod().getInstance());
			
		
		imageView.setImageDrawable(convertBlobToBitmap(blob));
		imageView.setPadding(10, 10, 10, 10);
		
		
				
		text1.setText(s1);	
		text2.setText(s2);
		text3.setText(s3);
	
		txttitle.setText(TenMonAn);	
		layoutcontent.addView(view);
		clickmonan.clearAnimation();
		clicknguyenlieu.clearAnimation();
		clickthuchien.clearAnimation();
		
		clickmonan.setOnClickListener(new View.OnClickListener() {			
			@Override
			public void onClick(View v) {
				layoutcontent.removeAllViews();
			 	layoutcontent.addView(view);
			 	if(clickmonan.isClickable()){
			 		clickmonan.setBackgroundColor(0xFFFFFFFF);
			 		clicknguyenlieu.setBackgroundResource(R.drawable.image_red);
			 		clickthuchien.setBackgroundResource(R.drawable.image_red);
			 	}
			}
		});
		
		
		clicknguyenlieu.setOnClickListener(new View.OnClickListener() {			
			@Override
			public void onClick(View v) {
				layoutcontent.removeAllViews();
				layoutcontent.addView(text2);				
				if(clicknguyenlieu.isClickable()){
					clicknguyenlieu.setBackgroundColor(0xFFFFFFFF);
			 		clickmonan.setBackgroundResource(R.drawable.image_red);
			 		clickthuchien.setBackgroundResource(R.drawable.image_red);
			 	}
			}
		});
		
		
		clickthuchien.setOnClickListener(new View.OnClickListener() {			
			@Override
			public void onClick(View v) {
				layoutcontent.removeAllViews();
				layoutcontent.addView(text3);
				if(clickthuchien.isClickable()){
					clickthuchien.setBackgroundColor(0xFFFFFFFF);
			 		clickmonan.setBackgroundResource(R.drawable.image_red);
			 		clicknguyenlieu.setBackgroundResource(R.drawable.image_red);
			 	}
			}
		});
		
		txtback.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				//finish();				
			}
		});
		
		imgmail.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(mcontent, PreparedMail.class);
				Bundle bundle = new Bundle();
				bundle.putString("contentmail", contentmail);
				intent.putExtras(bundle);
				mcontent.startActivity(intent);		
				
			}
		});
		viewgroup.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
				LayoutParams.FILL_PARENT));
		return viewgroup;
	}

	
	public BitmapDrawable convertBlobToBitmap(byte[] blobByteArray) {       
        Bitmap tempBitmap=null;        
        if(blobByteArray!=null)
        tempBitmap = BitmapFactory.decodeByteArray(blobByteArray, 0, blobByteArray.length);
        int width = tempBitmap.getWidth();
        int height = tempBitmap.getHeight();
        
        Matrix matrix = new Matrix();
        float scaleWidth = ((float) 400) / width;
        float scaleHeight = ((float) 200) / height;
        matrix.postScale(scaleWidth, scaleHeight);       
        matrix.postRotate(0); // rotate the Bitmap
        
        Bitmap resizingBitmap = Bitmap.createBitmap(tempBitmap,0,0,width,height,matrix,true); // recreate the new Bitmap
        
        BitmapDrawable bmd = new BitmapDrawable(resizingBitmap);
        
        return bmd;
    }
	
	
	
	// =======================================================================================

//	private Animation InfromRight() {
//		Animation inFromRight = new TranslateAnimation(
//		Animation.RELATIVE_TO_PARENT, +1.0f, Animation.RELATIVE_TO_PARENT,0.0f,
//		Animation.RELATIVE_TO_PARENT, 0.0f,	 Animation.RELATIVE_TO_PARENT, 0.0f
//		);
//		inFromRight.setDuration(500);	
//		inFromRight.setInterpolator(new AccelerateInterpolator());
//		return inFromRight;
//	}
//
//	private Animation OuttoLeft() {
//		Animation outtoLeft = new TranslateAnimation(
//		Animation.RELATIVE_TO_PARENT,  0.0f, Animation.RELATIVE_TO_PARENT,  -1.0f,
//		Animation.RELATIVE_TO_PARENT,  0.0f, Animation.RELATIVE_TO_PARENT,   0.0f
//		);
//		outtoLeft.setDuration(500);
//		outtoLeft.setInterpolator(new AccelerateInterpolator());
//		return outtoLeft;
//	}
//
//	 private Animation InfromLeft(){
//		 Animation inFromLeft = new TranslateAnimation(
//		 Animation.RELATIVE_TO_PARENT, -1.0f, Animation.RELATIVE_TO_PARENT,0.0f,
//		 Animation.RELATIVE_TO_PARENT, 0.0f,  Animation.RELATIVE_TO_PARENT, 0.0f
//		 );
//		 inFromLeft.setDuration(500);	
//		 inFromLeft.setInterpolator(new AccelerateInterpolator());
//		 return inFromLeft;
//	 }
//	
//	 private Animation OuttoRight(){
//		 
//		 Animation outtoRight = new TranslateAnimation(
//		 Animation.RELATIVE_TO_PARENT,  0.0f, Animation.RELATIVE_TO_PARENT,  +1.0f,
//		 Animation.RELATIVE_TO_PARENT,  0.0f, Animation.RELATIVE_TO_PARENT,   0.0f
//		 );
//		 outtoRight.setDuration(500);
//		 outtoRight.setInterpolator(new AccelerateInterpolator());
//		 return outtoRight;
//	 }

	// =======================================================================================
	 
//	 	  
//	 
//	@Override
//	public boolean onDown(MotionEvent e) {
//		// TODO Auto-generated method stub
//		return false;
//	}
//	
//	
//	public boolean onTouchEvent(MotionEvent event) {
//		// TODO Auto-generated method stub
//		return detector.onTouchEvent(event);
//	}
//
//	@Override
//	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
////			float velocityY) {
////		if(e1.getX() - e2.getX() > 0){
////			viewFlipper.setInAnimation(InfromRight());
////			viewFlipper.setOutAnimation(OuttoLeft());
////			viewFlipper.showNext();
////		}else{
////			viewFlipper.setInAnimation(InfromLeft());
////			viewFlipper.setOutAnimation(OuttoRight());
////			viewFlipper.showPrevious();
////		}			
////		return false;
//	}
//
//	@Override
//	public void onLongPress(MotionEvent e) {
//		// TODO Auto-generated method stub
//
//	}
//
//	@Override
//	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
//			float distanceY) {
//		// TODO Auto-generated method stub
//		return false;
//	}
//
//	@Override
//	public void onShowPress(MotionEvent e) {
//		// TODO Auto-generated method stub
//
//	}
//
//	@Override
//	public boolean onSingleTapUp(MotionEvent e) {
//		// TODO Auto-generated method stub
//		return false;
//	}
}
